package vlms.controller;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import vlms.bean.*;
import vlms.service.VLMSServiceProxy;
/**
 * Servlet implementation class SearchMovie
 */
public class SearchMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		String destination = null;
		 try
		 {
			 VLMSServiceProxy proxy = new VLMSServiceProxy();
			 Movie[] movieList = proxy.listAllMovies();
			 request.setAttribute("MovieList", movieList);
			 destination = "/searchMovie.jsp";
			
		 }
		 catch(RemoteException re)
		 {
			 destination = "/home.jsp";
			 request.setAttribute("message", re.getMessage());
			 re.printStackTrace();
		 }
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destination);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String destination = "/home.jsp";
		String message = null;
		
		if(request.getParameter("submit").equals("Search")) {
			Movie movie=new Movie();
			
			
			System.out.println(" SearchCriteria:" + request.getParameter("searchCriteria"));
			System.out.println("Values : " + request.getParameter("searchByText"));
			String searchCriteria = request.getParameter("searchCriteria");
			String searchByText = request.getParameter("searchByText");
			
			if(searchCriteria.equals("movieName")) 
				movie.setMoviename(searchByText);
			else if(searchCriteria.equals("banner")) 
				movie.setMoviebanner(searchByText);
			else 
				movie.setCategory(searchByText);
			
			
			try
			 {
				 VLMSServiceProxy proxy = new VLMSServiceProxy();
				 Movie[] movieList = proxy.retrieveMovies(movie);
				 request.setAttribute("MovieList", movieList);
				 destination = "/searchMovie.jsp";
			 }
			 catch(RemoteException re)
			 {
				 destination = "/searchMovie.jsp";
				 request.setAttribute("message", re.getMessage());
				 re.printStackTrace();
			 }
		}	
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destination);
		dispatcher.forward(request, response);
		 
		 
		
	}

}
